﻿var baseConfig = (function() {
    this.localHttp = 'http://oa.hsg.com.cn/'; //接口地址  lihuan.4kb.cn/192.168.86.6:8080/HSG_OA/192.168.80.9/HSG_OA/oa.hsg.com.cn
    this.localHttp2 = 'http://192.168.80.9/HSG_OA/'; //接口地址2
    this.imglocalHttp = "http://59.175.233.138/img/" //图片地址59.175.233.138/img//img.hsg.com.cn/
        //获取当前时间yyyy-mm-dd格式
    this.getNowFormatDate = function() {
            var date = new Date();
            var seperator1 = "-";
            var year = date.getFullYear();
            var month = date.getMonth() + 1;
            var strDate = date.getDate();
            if (month >= 1 && month <= 9) {
                month = "0" + month;
            }
            if (strDate >= 0 && strDate <= 9) {
                strDate = "0" + strDate;
            }
            var currentdate = year + seperator1 + month + seperator1 + strDate;
            return currentdate;
        }
        //页面取前一个页面传过来的参数值
    this.GetQueryString = function(name) { //页面取值方法
            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
            var r = window.location.search.substr(1).match(reg);
            if (r != null) return decodeURI(r[2]);
            return null;
        }
        //这里其实已经没用的，已经没有地方用到这里了，可以注释掉
    this.loadTB = function() {
            $("#header").load('../common/header.html', function() {});
            $("#footer").load('../common/footer.html', function() {
                $(".weui-tabbar a").on("click", function() {
                    $(this).addClass("weui-bar__item--on").siblings().removeClass("weui-bar__item--on")
                })
            })
        }
        //订单状态
    this.colorCode = function(status) {
            if (status == 100 || status == 60) {
                return "#f65555"
            }
            if (status == 20 || status == 10 || status == 40 || status == 70) {
                return "#fdb96e"
            }
            if (status == 0) {
                return "#28c81e"
            }
            if (status == 110) {
                return "#777777"
            }
        }
        //运输车状态
    this.colorTrans = function(status) {
            if (status == 0) {
                return "#f65555"
            }
            if (status == 1) {
                return "#28c81e"
            }
            if (status == 2 || status == 4) {
                return "#fdb96e"
            }
            if (status == 3) {
                return "#777777"
            }
        }
        //接口判断登录的，若没登录直接跳到登录页
    this.login = function() {
        var SupplierId = localStorage.getItem("SupplierId")
        if (!SupplierId) {
            $.confirm("尚未登录，是否去登录", function() {
                //点击确认后的回调函数
                window.location.href = "../login/login.html"
            }, function() {
                //点击取消后的回调函数
            });
        } else {
            return SupplierId
        }
    }
    this.getCode = function() {
            window.location.href = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx15210d09c0820450&redirect_uri=http://www.hsw.com/weixin/weChatSupplier/index.html&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect"
        }
    //www.hsw.com/weixin/weChatSupplier/index.html
    //oa.hsg.com.cn/static/weixinc/weChatSupplier/index.html
        //返回键的回到上一页
    this.historyBack = function() {
            $(".header-title .left").on("click", function() {
                history.back()
            })
        }
        //图片多张上传，删除，预览，一次传一张，可传多次
    this.imgUpload = function() {
            // 允许上传的图片类型
            //		var allowTypes = ['image/jpg', 'image/jpeg', 'image/png', 'image/gif'];
            // 1024KB，也就是 1MB
            //		var maxSize = 1024 * 1024;
            // 图片最大宽度
            var maxWidth = 1000;
            // 最大上传图片数量
            var maxCount = 6;
            var index = 0

            $('.weui-uploader__input-box').on('change', '.js_file', function(event) {
                var files = event.target.files;
                // 如果没有选中文件，直接返回				
                var $input = $("<input id='uploaderInput' name='file' class='weui-uploader__input js_file' type='file' accept='image/*'>")
                $(".weui-uploader__input-box").append($input);

                if (files.length === 0) {
                    return;
                }
                index++
                console.log(this)
                for (var i = 0, len = files.length; i < len; i++) {
                    var file = files[i];
                    var reader = new FileReader();

                    reader.onload = function(e) {
                        var img = new Image();
                        img.onload = function() {
                            // 不要超出最大宽度
                            var w = Math.min(maxWidth, img.width);
                            // 高度按比例计算
                            var h = img.height * (w / img.width);
                            var canvas = document.createElement('canvas');
                            var ctx = canvas.getContext('2d');
                            // 设置 canvas 的宽度和高度
                            canvas.width = w;
                            canvas.height = h;
                            ctx.drawImage(img, 0, 0, w, h);
                            var base64 = canvas.toDataURL('image/png');
                            // 插入到预览区
                            var $preview = $('<li class="weui-uploader__file weui-uploader__file_status"  style="overflow:hidden"><img src=' + base64 + ' class="baseImg"><img src="../../img/delete.png" class="delete" index=' + index + '  /><div class="weui-uploader__file-content">0%</div></li>');
                            $('.weui-uploader__files').append($preview);
                            // 然后假装在上传，可以post base64格式，也可以构造blob对象上传，也可以用微信JSSDK上传
                            // getOrientation(file, function(orientation) {
                            //     if (orientation == 6) {
                            //         $(".baseImg").eq($(".weui-uploader__file").length - 1).css("transform", "rotate(90deg)")
                            //         $(".baseImg").eq($(".weui-uploader__file").length - 1).attr("data-att", 1)
                            //     } else {
                            //         $(".baseImg").eq($(".weui-uploader__file").length - 1).css("transform", "rotate(0)")
                            //         $(".baseImg").eq($(".weui-uploader__file").length - 1).attr("data-att", 2)
                            //     }
                            // });
                            //图片预览
                            // $(".weui-uploader__files li").on("click", function() {
                            // console.log($(this).find(".baseImg").css("transform"))
                            // if ($(this).find(".baseImg").attr("data-att") == 1) {
                            //     $(".weui-gallery__img").css("transform", "rotate(90deg)")
                            // } else {

                            //     $(".weui-gallery__img").css("transform", "rotate(0)")
                            // }
                            // var src = $(this).find(".baseImg").attr("src")
                            var imgsurl = [];
                            var nowurl = '';
                            var imgObj = $(".weui-uploader__files li .baseImg");
                            for (var i = 0; i < imgObj.length; i++) {
                                imgsurl[i] = imgObj[i].src;
                                imgObj[i].onclick = function() {
                                    nowurl = this.src;
                                    wx.previewImage({
                                        current: nowurl,
                                        urls: imgsurl
                                    });
                                }
                            }
                            //     imgObj[i].onclick = function() {
                            // nowurl = this.src;
                            // wx.previewImage({
                            //     current: src,
                            //     urls: imgsurl
                            // });
                            // }

                            // $(".weui-gallery").show()
                            // $(".weui-gallery__img").css("background-image", "url(" + src + ")")
                            // $(".weui-gallery").on("click", function() {
                            //     $(this).hide()
                            // })
                            // })
                            //图片删除	
                            $(".weui-uploader__file .delete").on("click", function(event) {
                                    event.stopPropagation() //阻止事件冒泡行为
                                    var Index = $(this).attr("index")
                                    sessionStorage.setItem("Index", Index)
                                    var that = $(this)
                                    $.confirm("确定删除这张图片么？", function() {
                                        $(that).parents().find(".weui-uploader__input-box input").eq(sessionStorage.getItem("Index") - 1).remove()
                                        sessionStorage.removeItem("Index")
                                        $(that).parent().remove()
                                            //console.log($(".weui-uploader__input-box input"))
                                    }, function() {
                                        sessionStorage.removeItem("Index")
                                            //取消之后的回调
                                    })
                                })
                                //上传进度
                            var progress = 0;

                            function uploading() {
                                $preview.find('.weui-uploader__file-content').text(++progress + '%');
                                if (progress < 100) {
                                    setTimeout(uploading, 30);
                                } else {
                                    //如果是失败，塞一个失败图标
                                    $preview.find('.weui-uploader__file-content').html('<i class="weui_icon_warn"></i>');
                                    $preview.removeClass('weui-uploader__file_status')
                                        .find('.weui-uploader__file-content')
                                        .remove();
                                }
                            }
                            setTimeout(uploading, 30);
                        };
                        img.src = e.target.result;
                    };
                    reader.readAsDataURL(file);
                }

            });
        }
        //图片单张上传
    this.singleImgupload = function() {
        // 允许上传的图片类型
        var allowTypes = ['image/jpg', 'image/jpeg', 'image/png', 'image/gif'];
        // 1024KB，也就是 1MB
        //		var maxSize = 1024 * 1024;
        // 图片最大宽度
        var maxWidth = 1000;
        // 最大上传图片数量
        var maxCount = 1;
        $('.js_file').on('change', function(event) {
            var files = event.target.files;
            // 如果没有选中文件，直接返回	

            if (files.length === 0) {
                return;
            }
            for (var i = 0, len = files.length; i < len; i++) {
                var file = files[i];
                var reader = new FileReader();
                // 如果类型不在允许的类型范围内
                //				if(allowTypes.indexOf(file.type) === -1) {
                //					$.weui.alert({
                //						text: '该类型不允许上传'
                //					});
                //					continue;
                //				}
                //				if(file.size > maxSize) {
                //					$.weui.alert({
                //						text: '图片太大，不允许上传'
                //					});
                //					continue;
                //				}
                //				if($('.weui_uploader__file').length >= 1) {
                //					$.weui.alert({
                //						text: '最多只能上传' + maxCount + '张图片'
                //					});
                //					return;
                //				}
                reader.onload = function(e) {
                    var img = new Image();
                    img.onload = function() {
                        // 不要超出最大宽度
                        var w = Math.min(maxWidth, img.width);
                        // 高度按比例计算
                        var h = img.height * (w / img.width);
                        var canvas = document.createElement('canvas');
                        var ctx = canvas.getContext('2d');
                        // 设置 canvas 的宽度和高度
                        canvas.width = w;
                        canvas.height = h;
                        ctx.drawImage(img, 0, 0, w, h);
                        var base64 = canvas.toDataURL('image/png');
                        // 插入到预览区
                        var $preview = $('<li class="weui-uploader__file weui-uploader__file_status" style="overflow:hidden"><img src=' + base64 + ' class="baseImg"><img src="../../img/delete.png" class="delete"/><div class="weui-uploader__file-content">0%</div></li>');
                        $('.weui-uploader__files').append($preview);

                        // 然后假装在上传，可以post base64格式，也可以构造blob对象上传，也可以用微信JSSDK上传
                        // getOrientation(files[0], function(orientation) {
                        //     if (orientation == 6) {
                        //         $(".baseImg").css("transform", "rotate(90deg)")
                        //         $(".weui-gallery__img").css("transform", "rotate(90deg)")
                        //     } else {
                        //         $(".baseImg").css("transform", "rotate(0)")
                        //         $(".weui-gallery__img").css("transform", "rotate(0)")
                        //     }
                        // });
                        // //图片预览
                        // $(".weui-uploader__files li").on("click", function() {
                        //         var src = $(this).find(".baseImg").attr("src")
                        //         $(".weui-gallery").show()
                        //         $(".weui-gallery__img").css("background-image", "url(" + src + ")")
                        //         $(".weui-gallery").on("click", function() {
                        //             $(this).hide()
                        //         })
                        //     })
                        var imgsurl = [];
                        var nowurl = '';
                        var imgObj = $(".weui-uploader__files li .baseImg");
                        for (var i = 0; i < imgObj.length; i++) {
                            imgsurl[i] = imgObj[i].src;
                            imgObj[i].onclick = function() {
                                nowurl = this.src;
                                wx.previewImage({
                                    current: nowurl,
                                    urls: imgsurl
                                });
                            }
                        }
                        //图片删除	
                        $(".weui-uploader__file .delete").on("click", function(event) {
                                event.stopPropagation() //阻止事件冒泡行为
                                var Index = $(this).attr("index")
                                sessionStorage.setItem("Index", Index)
                                var that = $(this)
                                $.confirm("确定删除这张图片么？", function() {
                                    $(that).parents().find(".weui-uploader__input-box input").eq(sessionStorage.getItem("Index") - 1).remove()
                                    sessionStorage.removeItem("Index")
                                    $(that).parent().remove()
                                    $(".weui-uploader__input-box").show()
                                        //console.log($(".weui-uploader__input-box input"))
                                }, function() {
                                    sessionStorage.removeItem("Index")
                                        //取消之后的回调
                                })
                            })
                            //上传进度
                        var progress = 0;

                        function uploading() {
                            $preview.find('.weui-uploader__file-content').text(++progress + '%');
                            if (progress < 100) {
                                setTimeout(uploading, 30);
                            } else {
                                //如果是失败，塞一个失败图标
                                $preview.find('.weui-uploader__file-content').html('<i class="weui_icon_warn"></i>');
                                $preview.removeClass('weui-uploader__file_status')
                                    .find('.weui-uploader__file-content')
                                    .remove();
                            }
                        }
                        setTimeout(uploading, 30);
                    };
                    img.src = e.target.result;
                };
                reader.readAsDataURL(file);
            }
            setTimeout(function() {
                $(".weui-uploader__input-box").hide()
            }, 20)
        });
    }

    function getOrientation(file, callback) {
        var reader = new FileReader();
        reader.onload = function(e) {
            var view = new DataView(e.target.result);
            if (view.getUint16(0, false) != 0xFFD8) return callback(-2);
            var length = view.byteLength,
                offset = 2;
            while (offset < length) {
                var marker = view.getUint16(offset, false);
                offset += 2;
                if (marker == 0xFFE1) {
                    if (view.getUint32(offset += 2, false) != 0x45786966) return callback(-1);
                    var little = view.getUint16(offset += 6, false) == 0x4949;
                    offset += view.getUint32(offset + 4, little);
                    var tags = view.getUint16(offset, little);
                    offset += 2;
                    for (var i = 0; i < tags; i++)
                        if (view.getUint16(offset + (i * 12), little) == 0x0112)
                            return callback(view.getUint16(offset + (i * 12) + 8, little));
                } else if ((marker & 0xFF00) != 0xFF00) break;
                else offset += view.getUint16(offset, false);
            }
            return callback(-1);
        };
        reader.readAsArrayBuffer(file);
    }
    return this
})();